package io.webchat.renthouse.controller;

import com.github.pagehelper.PageInfo;
import io.webchat.annotation.Login;
import io.webchat.minio.MinloUploadService;
import io.webchat.n3r.SnowflakeUtil;
import io.webchat.renthouse.pojo.House;
import io.webchat.renthouse.pojo.HouseImage;
import io.webchat.renthouse.services.IHouseImageService;
import io.webchat.renthouse.services.IHouseService;
import io.webchat.renthouse.vo.AddHouse;
import io.webchat.renthouse.vo.HouseVo;
import io.webchat.wdzl.utils.IWdzlJSONResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.*;


@RestController
@RequestMapping("api/rentHouse")
@Slf4j
public class AddHouseController {



	@Autowired
	private IHouseService houseService;

	@Autowired
	MinloUploadService minloUploadService;


	@Autowired
	IHouseImageService iHouseImageService;

	@PostMapping("/updateHouseImage")
	@Login
	public IWdzlJSONResult upload(@RequestParam("file") List<MultipartFile> files) {
		List<String> urlImages = new ArrayList<>();
		if (!files.isEmpty() && files.size() > 0) {
			for (MultipartFile file : files) {
				// 文件名
				String urlImage = minloUploadService.uploadImg(file, "renthouse");
				urlImages.add(urlImage);
			}
		}
		return  IWdzlJSONResult.ok().putValue(urlImages);
	}


	@PostMapping("/addHouseRecord")
	@Login
	public IWdzlJSONResult addHouse(@RequestBody AddHouse addHouse) {
		House house = new House();
		BeanUtils.copyProperties(addHouse,house);
		String houseId =SnowflakeUtil.getId();
		house.setId(houseId);
		house.setPublishTime(new Date());
		houseService.addNewHouse(house);

		//保存首页图片
		String houseHomeImageId =SnowflakeUtil.getId();
		HouseImage homeImage = new HouseImage();
		homeImage.setId(houseHomeImageId);
		homeImage.setImageUrl(addHouse.getHouseImage());
		homeImage.setHouseId(houseId);
		homeImage.setType(1);
		homeImage.setCreateBy(addHouse.getPublisher());
		homeImage.setCreateDate(new Date());
		iHouseImageService.addHouseImage(homeImage);



		//保存房屋详情图片
		String[] imageUrlList = addHouse.getHouseDetailsImg().split(",");
		for(String imageUrl:imageUrlList){
			String houseDetailImageId =SnowflakeUtil.getId();
			HouseImage houseDetailImage = new HouseImage();
			houseDetailImage.setId(houseDetailImageId);
			houseDetailImage.setImageUrl(imageUrl);
			houseDetailImage.setHouseId(houseId);
			houseDetailImage.setType(2);
			houseDetailImage.setCreateBy(addHouse.getPublisher());
			houseDetailImage.setCreateDate(new Date());
			iHouseImageService.addHouseImage(houseDetailImage);
		}
		return  IWdzlJSONResult.ok();
	}


	@PostMapping("/selectHouses")
	@Login
	public IWdzlJSONResult selectHouse(@RequestBody HouseVo houseVo) {

		PageInfo<House> housePageInfo = houseService.selectHouse(houseVo);
		return  IWdzlJSONResult.ok().putValue(housePageInfo);
	}
}
